<template>
  <el-dialog
    title="收货地址"
    :title="options.title"
    :visible.sync="options.visible">
    <el-form :model="form" :rules="rules" ref="form">
      <el-form-item
        label="算子类名"
        :label-width="formLabelWidth"
        prop="classname">
        <el-input v-model="form.classname" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="算子名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
          label="描述"
          :label-width="formLabelWidth"
          prop="description">
        <el-input v-model="form.description" autocomplete="off"></el-input>
      </el-form-item>
<!--      <el-form-item label="类型" :label-width="formLabelWidth" prop="type">-->
<!--        <el-select v-model="form.type" placeholder="请选择" style="width: 100%">-->
<!--          <el-option label="全部" :value="0"></el-option>-->
<!--          <el-option label="类别1" :value="1"></el-option>-->
<!--          <el-option label="类别2" :value="2"></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="标签" :label-width="formLabelWidth" prop="tag_id">
        <el-select
          v-model="form.tag_id"
          placeholder="请选择"
          style="width: 100%">
          <el-option
            v-for="item in tagItem"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="方法名称" :label-width="formLabelWidth" prop="function">
        <el-input v-model="form.function" autocomplete="off"></el-input>
      </el-form-item>
            <el-form-item label="来源字段类型" :label-width="formLabelWidth" prop="fromfieldtype">
              <el-select v-model="form.fromfieldtype" placeholder="请选择" style="width: 100%">
                <el-option
                    v-for="item in fromItem"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
      <el-form-item label="目标字段类型" :label-width="formLabelWidth" prop="tofieldtype">
        <el-select v-model="form.tofieldtype" placeholder="请选择" style="width: 100%">
          <el-option
              v-for="item in toItem"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="路径" :label-width="formLabelWidth" prop="path">
        <el-input v-model="form.path" autocomplete="off" disabled></el-input>
        <div>
          <input
            type="file"
            @change="handleFileUpload"
            style="display: none"
            ref="fileInput" />
          <el-button @click="selectFile">选择算子</el-button>
        </div>
        <div style="display: none">{{ filetext }}</div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="confirm"> 确 定 </el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    props: {
      options: {
        type: Object,
        default() {
          return {};
        },
      },
      row: {
        type: Object,
        default() {
          return null;
        },
      },
    },
    data() {
      return {
        tagItem: [
          { id: 2, name: "test1" },
          { id: 3, name: "test2" },
        ],
        form: {},
        rules: {
          classname: [
            { required: true, message: "请输入算子类名", trigger: "blur" },
          ],
          name: [
            { required: true, message: "请输入算子名称", trigger: "blur" },
          ],
          description: [
            { required: true, message: "请输入描述", trigger: "blur" },
          ],
          // params: [{ required: true, message: "请输入参数", trigger: "blur" }],
          // path: [
          //   { required: true, message: "请输入路径", trigger: "blur" },
          //   // {
          //   //   min: 1,
          //   //   max: 18,
          //   //   message: "长度在 1 到 18 个字符",
          //   //   trigger: "blur",
          //   // },
          // ],
          // type: [
          //   {
          //     required: true,
          //     message: "请至少选择一个类型",
          //     trigger: "change",
          //   },
          // ],
        },
        formLabelWidth: "100px",
        filePath: "",
        filetext: "",
        fromItem: [
          { value: "JSONArray", label: "JSONArray" },
          { value: "JSONObject", label: "JSONObject" },
          { value: "String", label: "String" },
          { value: "Integer", label: "Integer" },
          { value: "Float", label: "Float" },
          { value: "Double", label: "Double" },
          { value: "Short", label: "Short" },
          { value: "Boolean", label: "Boolean" },
          { value: "Long", label: "Long" },
          { value: "Byte", label: "Byte" },
        ],
        toItem: [
          { value: "JSONArray", label: "JSONArray" },
          { value: "JSONObject", label: "JSONObject" },
          { value: "String", label: "String" },
          { value: "Integer", label: "Integer" },
          { value: "Float", label: "Float" },
          { value: "Double", label: "Double" },
          { value: "Short", label: "Short" },
          { value: "Boolean", label: "Boolean" },
          { value: "Long", label: "Long" },
          { value: "Byte", label: "Byte" },
        ],
      };
    },
    created() {
      this.init();
      this.getTagAll();
    },
    methods: {
      //查询所有标签
      getTagAll() {
        let result = window.iTag.getAll();
        let res = JSON.parse(result);
        this.tagItem = res.data;
      },
      selectFile() {
        // this.$refs.fileInput.click();
        let result =window.iOperator.upload();
        let res =JSON.parse(result)
        this.form.path = res.data;
        this.filetext =res.data;
      },
      handleFileUpload(event) {
        // const file = event.target.files[0];
        // if (!file) {
        //   alert("请选择一个文件");
        //   return;
        // }
        // this.filePath = file.name;
        // console.log("🚀 ~ handleFileUpload ~ this.filePath:", this.filePath);
        // let result = window.iOperator.upload(this.filePath);
        // let res = JSON.parse(result);
        // this.filetext = res.data;
        // this.form.path = res.data;
        // // if (code == 1) {
        // //   this.$message.success(res.msg);
        // // } else {
        // //   this.$message.error(res.msg);
        // // }
        // this.$refs.fileInput.value = null;
        //
        // // 检查文件类型是否为 JSON
        // // const fileExtension = file.name.split(".").pop().toLowerCase();
        // // if (fileExtension !== "json") {
        // //   this.$message.error("只允许上传JSON格式的文件");
        // //   return;
        // // } else {
        // //   this.filePath = file.name;
        // //   this.form.path = 111;
        // //   console.log("🚀 ~ handleFileUpload ~ this.filePath:", this.filePath);
        // //   // const fileName = file.name.split("\\").pop();
        // //   // this.fileName = fileName.split(".")[0];
        // //   // let item = {
        // //   //   // key: this.fileName2,
        // //   //   key: "MetaB",
        // //   //   path: this.filePath2,
        // //   // };
        // //   // let itemAdd = JSON.stringify(item);
        // //   // let result = window.iTest.upload(itemAdd);
        // //   // let res = JSON.parse(result);
        // //   // if (res.code == 1) {
        // //   //   this.$message.success(res.msg);
        // //   // } else {
        // //   //   this.$message.error(res.msg);
        // //   // }
        // //   // this.$refs.fileInput2.value = null;
        // // }
      },
      init() {
        if (this.row) {
          this.form = JSON.parse(JSON.stringify(this.row));
          console.log("🚀 ~ init ~ this.form:", this.form);
        }
      },
      confirm() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.row
              ? this.$emit("onEditClose", this.form)
              : this.$emit("onAddClose", this.form);
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      close() {
        this.row ? this.$emit("onEditClose") : this.$emit("onAddClose");
      },
    },
  };
</script>

<style scoped>
  .el-dialog {
    /* width: 500px !important; */
    padding-right: 30px;
  }
</style>
